package com.raychi.demo.action;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.raychi.demo.entity.Company;
import com.raychi.demo.service.CompanyService;
import com.raychi.demo.util.CompanyUtil;
import com.raychi.demo.util.JdbcUtil;

public class CompanyAction extends ActionSupport {

	private static final long serialVersionUID = 8161043236284179864L;
	
	private List<Company> list;
	private CompanyService companyService;
	private Company company;
	
	public String list() throws Exception {
		list = companyService.getCompanyList();
		return "list";
	}
	
	public String input() {
		
		return "input";
	}
	
	public String edit(){
		
		return "edit";
	}
	public String update() throws Exception{
		companyService.update(company);
		return list();
	}
	
	public String delete() throws Exception{
		companyService.delete(company);
		return list();
	}
	
	public String save() throws Exception{
		companyService.save(company);
		return list();
		
	}
	
	public Company getCompanyById(int id) {
		Company company = null;
		Connection conn = null;
		try {
			conn = JdbcUtil.getConnection();
			Statement st = conn.createStatement();
			String sql = "select * from company where id=" + id;
			ResultSet rs = st.executeQuery(sql);
			while (rs.next()) {
				company = CompanyUtil.createCompany(rs);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (conn != null)
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
		return company;
		
	}
	

	public List<Company> getList() {
		return list;
	}

	public void setList(List<Company> list) {
		this.list = list;
	}



	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}
}
